package l1j.server.server.model.damage;

import static l1j.server.server.model.skill.L1SkillId.BONE_BREAK_START;
import static l1j.server.server.model.skill.L1SkillId.BONE_BREAK;
import static l1j.server.server.model.skill.L1SkillId.BERSERKERS;
import static l1j.server.server.model.skill.L1SkillId.BURNING_SLASH;
import static l1j.server.server.model.skill.L1SkillId.BURNING_SPIRIT;
import static l1j.server.server.model.skill.L1SkillId.BURNING_WEAPON;
import static l1j.server.server.model.skill.L1SkillId.COOKING_2_0_N;
import static l1j.server.server.model.skill.L1SkillId.COOKING_2_0_S;
import static l1j.server.server.model.skill.L1SkillId.COOKING_2_3_N;
import static l1j.server.server.model.skill.L1SkillId.COOKING_2_3_S;
import static l1j.server.server.model.skill.L1SkillId.COOKING_3_0_N;
import static l1j.server.server.model.skill.L1SkillId.COOKING_3_0_S;
import static l1j.server.server.model.skill.L1SkillId.COOKING_3_2_N;
import static l1j.server.server.model.skill.L1SkillId.COOKING_3_2_S;
import static l1j.server.server.model.skill.L1SkillId.DOUBLE_BRAKE;
import static l1j.server.server.model.skill.L1SkillId.ELEMENTAL_FIRE;
import static l1j.server.server.model.skill.L1SkillId.ENCHANT_VENOM;
import static l1j.server.server.model.skill.L1SkillId.FIRE_BLESS;
import static l1j.server.server.model.skill.L1SkillId.FIRE_WEAPON;
import static l1j.server.server.model.skill.L1SkillId.ILLUSION_AVATAR;
import static l1j.server.server.model.skill.L1SkillId.SOUL_OF_FLAME;
import static l1j.server.server.model.skill.L1SkillId.SPECIAL_EFFECT_WEAKNESS_LV1;
import static l1j.server.server.model.skill.L1SkillId.SPECIAL_EFFECT_WEAKNESS_LV2;
import static l1j.server.server.model.skill.L1SkillId.SPECIAL_EFFECT_WEAKNESS_LV3;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.GeneralThreadPool;
import l1j.server.server.datatables.ClanSkillTable;
import l1j.server.server.datatables.EnchantWeaponAdvance;
import l1j.server.server.datatables.EnchantWeaponAdvance.WeaponEnchantAdvance;
import l1j.server.server.datatables.WeaponEffectTable;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1EffectSpawn;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.poison.L1DamagePoison;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_EffectLocation;
import l1j.server.server.serverpackets.S_OwnCharStatus;
import l1j.server.server.serverpackets.S_SPMR;
import l1j.server.server.serverpackets.S_SkillIconGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1MagicDoll;
import l1j.server.server.utils.Random;
import l1j.server.server.utils.SQLUtil;

public class L1PcDamage {
	private static final int[] strDmg = new int[255];
	private static final int[] dexDmg = new int[255];
	static {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("select * from x_str_dex");
			rs = pstm.executeQuery();
			while (rs.next()) {
				int i = rs.getInt("value") - 1;
				strDmg[i] = rs.getInt("str_dmg");
				dexDmg[i] = rs.getInt("dex_dmg");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
			SQLUtil.close(rs);
		}
	}

	private L1Dmg _dmg;
	private L1PcInstance _attacker;
	private L1Character _target;
	private L1ItemInstance weapon;

	public L1PcDamage(L1Dmg dmg, L1PcInstance attacker, L1Character target) {
		_attacker = attacker;
		_target = target;
		_dmg = dmg;
		weapon = _attacker.getWeapon();
	}

	public void calcPcDmg() {
		if (weapon == null) {
			_dmg.add(Random.nextInt(5, 4) / 4).add(strDmg[_attacker.getStr()]);
		} else {
			if (weapon.isBow() || weapon.isGauntlet()) {
				_dmg.add(dexDmg[_attacker.getDex()])
						.add(_attacker.getBowDmgup())
						.add(_attacker.getOriginalBowDmgup())
						.add(_attacker.getBowDmgModifierByArmor());
				if (_attacker.hasSkillEffect(COOKING_2_3_N) // 料理
						|| _attacker.hasSkillEffect(COOKING_2_3_S)
						|| _attacker.hasSkillEffect(COOKING_3_0_N)
						|| _attacker.hasSkillEffect(COOKING_3_0_S))
					_dmg.add(1);
			} else {
				if (weapon.isKiringku()) {
					calcKiringkuDmg();
				} else {
					_dmg.add(strDmg[_attacker.getStr()])
							.add(_attacker.getDmgup())
							.add(_attacker.getOriginalDmgup());
					WeaknessExposure();
					calcBuffDamage();
					_dmg.add(_attacker.getDmgModifierByArmor());
				}
				if (_attacker.hasSkillEffect(COOKING_2_0_N) // 料理
						|| _attacker.hasSkillEffect(COOKING_2_0_S)
						|| _attacker.hasSkillEffect(COOKING_3_2_N)
						|| _attacker.hasSkillEffect(COOKING_3_2_S))
					_dmg.add(1);
			}
			// 骷髏毀壞判斷
			if (_attacker.isBONE_BREAK()) {// 骷髏毀壞
				_dmg.add(10);
				if (!_target.hasSkillEffect(BONE_BREAK)
						&& Random.chance100(Random.nextInt(11, 20))) {
					L1EffectSpawn.getInstance().spawnEffect(93001, 1700,
							_target.getX(), _target.getY(), _target.getMapId());
					_target.setSkillEffect(BONE_BREAK, 2 * 1000); // 發動後再次發動間隔
																	// 2秒
					_target.setSkillEffect(BONE_BREAK_START, 700);
				}
			} else if (_attacker.isSMASH()) {// 爆擊
				_dmg.add(15);
			}
			int enchantLevel = weapon.getEnchantLevel();
			

			WeaponEnchantAdvance wea = EnchantWeaponAdvance.getInstance()
					.getAdvance(enchantLevel);
			_dmg.add(wea.getExtra_phy_dmg());
			if (Random.chance100(wea.getCritical_phy_chance()))
				_dmg.mul(1.0 + wea.getCritical_phy_rate() / 100.0);

			// 輔助裝備（符石）增加傷害
			double mul = 1.0;
			for (L1ItemInstance armors : _attacker.getEquipSlot().getArmors()) {
				mul += armors.getRuneDmgPercent() / 100.0;
			}
			_dmg.mul(mul);

			// ItemExtend
			_dmg.add(_attacker.getIe_Dmg());
			
			// 取得公會技能效果(clanskill_dmg)
			if (_attacker.getClanid() != 0 && _attacker.getClan().getClanSkill_dmg() != 0) {
				String[] dmgValueStr = ClanSkillTable.getInstance().getClanSkillDetail(1).getClan_skill_value();
				double dmgValue = Double.valueOf(dmgValueStr[_attacker.getClan().getClanSkill_dmg() - 1]);
				_dmg.add(dmgValue);
			}
			
			// 新增2倍暴擊率 Critical && 【狂】
			if (Random.chance100(_attacker.getCritical())) {
				_dmg.mul(2);
			}
			
			// 烙印裝備符文系統 - 【虛】【顛】【撕】
			if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_5)) {
				double subDmg = _attacker.get_SubRuneSetEffect();
				if (_dmg.getValue() <= subDmg) {
					subDmg = _dmg.getValue() - 1;
				}
				_dmg.sub(subDmg);
//				_attacker.sendPackets(new S_SystemMessage("【虛】發動 - 被扣除物攻:" + subDmg));
			}
			if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF)
					&& _attacker.getRemindRuneSetId() == 7) {
				_dmg.add(_attacker.getRemindBuffValue());
//				_attacker.sendPackets(new S_SystemMessage("【顛】發動 - 增加傷害:" + _attacker.getRemindBuffValue()));
			}
			if (_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_8)
					&& Random.chance100(33)) {
				_dmg.mul(1.5);
//				_attacker.sendPackets(new S_SystemMessage("【撕】發動 - 造成1.5倍傷害。"));
			}
		}
	}

	public void calcWeaponDmg(boolean small) {
		int weaponDmg = 0;
		if (weapon != null) {
			int weaponMax = weapon.getItem().getDmgLarge();
			if (small) {
				weaponMax = weapon.getItem().getDmgSmall();
			}
			if (weapon.isBow()) { // 弓
				L1ItemInstance arrow = _attacker.getInventory().getArrow();
				if (arrow != null) {
					if (small) {
						weaponDmg = Random.nextInt(arrow.getItem()
								.getDmgSmall(), 1);
					} else {
						weaponDmg = Random.nextInt(arrow.getItem()
								.getDmgLarge(), 1);
					}
				} else {
					if (weapon.getItemId() == 190) { // 沙哈之弓
						weaponDmg = Random.nextInt(15, 1);
					}
				}
			} else if (weapon.isGauntlet()) { // 飛刀
				L1ItemInstance sting = _attacker.getInventory().getSting();
				if (sting != null) {
					if (small) {
						weaponDmg = Random.nextInt(sting.getItem()
								.getDmgSmall(), 1);
					} else {
						weaponDmg = Random.nextInt(sting.getItem()
								.getDmgLarge(), 1);
					}
				}
			} else {
				weaponDmg = Random.nextInt(weaponMax, 1);
				if (_attacker.hasSkillEffect(SOUL_OF_FLAME)) { // 烈燄之魂
					weaponDmg = weaponMax;
				}
				if (Random.chance100(weapon.getItem().getDoubleDmgChance())) { // 武器雙擊
					if (weapon.isClaw()) {
						weaponDmg = weaponMax;
						_attacker.sendPackets(new S_SkillSound(_attacker
								.getId(), 3671));
						_attacker.broadcastPacket(new S_SkillSound(_attacker
								.getId(), 3671));
					}
					if (weapon.isEdoryu()) {
						weaponDmg *= 2;
						_attacker.sendPackets(new S_SkillSound(_attacker
								.getId(), 3398));
						_attacker.broadcastPacket(new S_SkillSound(_attacker
								.getId(), 3398));
					}
				}
			}
			weaponDmg += weapon.getEnchantLevel()
					+ weapon.getItem().getDmgModifier()
					+ weapon.getDmgByMagic();
			if (weapon.isEdoryu() || weapon.isClaw()) {// 雙重破壞
				if (_attacker.hasSkillEffect(DOUBLE_BRAKE)
						&& Random.chance100(33)) {
					weaponDmg *= 2;
				}
			}
			_dmg.add(weaponDmg);

			// 武器屬性階位傷害
			if (weapon.getAttrEnchantKind() != 0 && weapon.isEquipped()) {
				L1Dmg attrEnchantDmg = L1Dmg.get(true, weapon.getAttrEnchantKind(),
						_attacker, _target);
				attrEnchantDmg.commitAttrByWp(_dmg.getValue());
				_dmg.add(attrEnchantDmg.getValue());
				int castgfx = 0;
				switch(weapon.getAttrEnchantKind()) {
				case 1:
					castgfx = 7514; // 聲音5890
					if (weapon.getAttrEnchantLevel() >= 7) {
						castgfx = 6599;
					}
					break;
				case 2:
					castgfx = 1811;
					if (weapon.getAttrEnchantLevel() >= 7) {
						castgfx = 4162;
					}
					break;
				case 4:
					castgfx = 5975;
					if (weapon.getAttrEnchantLevel() >= 7) {
						castgfx = 3778;
					}
					break;
				case 8:
					castgfx = 2720;
					if (weapon.getAttrEnchantLevel() >= 7) {
						castgfx = 5528;
					}
					break;
				}
				_attacker.sendPackets(new S_SkillSound(_target.getId(), castgfx));
				_attacker.broadcastPacket(new S_SkillSound(_target.getId(), castgfx));
			}
			// 烙印裝備符文系統
			if (weapon.getBrandRuneSetId() != 0) {
				effectBrandRuneSet();
			}
//			L1Dmg attrEnchantDmg = L1Dmg.get(true, weapon.getAttrEnchantKind(),
//					_attacker, _target);
//			attrEnchantDmg.add(weapon.getAttrEnchantLevel() * 2 - 1);
//			attrEnchantDmg.commitMagic(_target);
//			_dmg.add(attrEnchantDmg.getValue());

		}

	}

	/** 弱點曝光 */
	private void WeaknessExposure() {
		if (weapon != null) {
			int random = Random.nextInt(100) + 1;
			if (weapon.isChainSword()) { // 鎖鏈劍
				// 使用屠宰者...
				if (_attacker.isFoeSlayer()) {
					return;
				}
				if (_attacker.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV3)) { // 目前階段三
					if (random > 30 && random <= 60) { // 階段三
						_attacker
								.killSkillEffectTimer(SPECIAL_EFFECT_WEAKNESS_LV3);
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV3,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 3));
					}
				} else if (_attacker
						.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV2)) { // 目前階段二
					if (random <= 30) { // 階段二
						_attacker
								.killSkillEffectTimer(SPECIAL_EFFECT_WEAKNESS_LV2);
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV2,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 2));
					} else if (random >= 70) { // 階段三
						_attacker
								.killSkillEffectTimer(SPECIAL_EFFECT_WEAKNESS_LV2);
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV3,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 3));
					}
				} else if (_attacker
						.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV1)) { // 目前階段一
					if (random <= 40) { // 階段一
						_attacker
								.killSkillEffectTimer(SPECIAL_EFFECT_WEAKNESS_LV1);
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV1,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 1));
					} else if (random >= 70) { // 階段二
						_attacker
								.killSkillEffectTimer(SPECIAL_EFFECT_WEAKNESS_LV1);
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV2,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 2));
					}
				} else {
					if (random <= 40) { // 階段一
						_attacker.setSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV1,
								16 * 1000);
						_attacker.sendPackets(new S_SkillIconGFX(75, 1));
					}
				}
			}
		}
	}

	// ●●●● 強化魔法近戰用 ●●●●
	private void calcBuffDamage() {
		// 火武器、バーサーカーのダメージは1.5倍しない
		if (_attacker.hasSkillEffect(BURNING_SPIRIT)
				|| _attacker.hasSkillEffect(ELEMENTAL_FIRE)) {
			if ((Random.nextInt(100) + 1) <= 33) {
				double tempDmg = _dmg.getValue();
				if (_attacker.hasSkillEffect(FIRE_WEAPON)) {
					tempDmg -= 4;
				} else if (_attacker.hasSkillEffect(FIRE_BLESS)) {
					tempDmg -= 5;
				} else if (_attacker.hasSkillEffect(BURNING_WEAPON)) {
					tempDmg -= 6;
				}
				if (_attacker.hasSkillEffect(BERSERKERS)) {
					tempDmg -= 5;
				}
				double diffDmg = _dmg.getValue() - tempDmg;
				_dmg.mul(1.5).add(diffDmg);
			}
		}
		// 鎖鏈劍
		if (weapon.isChainSword()) {
			// 弱點曝光 - 傷害加成
			if (_attacker.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV3)) {
				_dmg.add(9);
			} else if (_attacker.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV2)) {
				_dmg.add(6);
			} else if (_attacker.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV1)) {
				_dmg.add(3);
			}
		}
		// 屠宰者 & 弱點曝光LV3 - 傷害 *1.3
		if (_attacker.isFoeSlayer()
				&& _attacker.hasSkillEffect(SPECIAL_EFFECT_WEAKNESS_LV3)) {
			_dmg.mul(1.3);
		}
		if (_attacker.hasSkillEffect(BURNING_SLASH)) { // 燃燒擊砍
			_dmg.add(10);
			_attacker.sendPackets(new S_EffectLocation(_target.getX(), _target
					.getY(), 6591));
			_attacker.broadcastPacket(new S_EffectLocation(_target.getX(),
					_target.getY(), 6591));
			_attacker.killSkillEffectTimer(BURNING_SLASH);
		}

	}

	private void calcKiringkuDmg() {
		int dmg = 0;
		int dice = 5;
		int diceCount = 2;
		int value = 0;
		int kiringkuDamage = 0;
		int charaIntelligence = 0;
		if (weapon.getItemId() == 270) {
			value = 16;
		} else {
			value = 14;
		}

		for (int i = 0; i < diceCount; i++) {
			kiringkuDamage += (Random.nextInt(dice) + 1);
		}
		kiringkuDamage += value;

		int spByItem = _attacker.getSp() - _attacker.getTrueSp(); // アイテムによるSP変動
		charaIntelligence = _attacker.getInt() + spByItem - 12;
		if (charaIntelligence < 1) {
			charaIntelligence = 1;
		}
		double kiringkuCoefficientA = (1.0 + charaIntelligence * 3.0 / 32.0);

		kiringkuDamage *= kiringkuCoefficientA;

		double kiringkuFloor = Math.floor(kiringkuDamage);

		dmg += kiringkuFloor + weapon.getEnchantLevel()
				+ _attacker.getOriginalMagicDamage();

		if (_attacker.hasSkillEffect(ILLUSION_AVATAR)) {
			dmg += 10;
		}

		_dmg.zero();
		_dmg.add(dmg);

		if (_attacker.getWeapon().getItem().getItemId() == 270) {
			_attacker.sendPackets(new S_SkillSound(_attacker.getId(), 6983));
			_attacker
					.broadcastPacket(new S_SkillSound(_attacker.getId(), 6983));
		} else {
			_attacker.sendPackets(new S_SkillSound(_attacker.getId(), 7049));
			_attacker
					.broadcastPacket(new S_SkillSound(_attacker.getId(), 7049));
		}
	}
	
	// 擊中回血
	public void hitRegenerationHp() {
		int hitHpr = _attacker.getHitRegenerationHpByArmor();
		if (weapon != null && hitHpr > 0 && _dmg.getValue() > 0) {
			if(weapon.isBow() || weapon.isGauntlet()) {
				hitHpr = hitHpr / 2;
			}
			int newHp = _attacker.getCurrentHp() + hitHpr;
			if (newHp > _attacker.getMaxHp()) {
				newHp = _attacker.getMaxHp();
			}
			if (newHp <= 0) {
				newHp = 0;
			}
			_attacker.setCurrentHp(newHp);
		}
	}

	// 吸魔
	public void effectManaDrain() {
		if (weapon != null) {
			int weaponId = weapon.getItemId();
			if (_dmg.getValue() > 0) {
				if (weaponId == 126 || weaponId == 127) {
					int drainMana = Random.nextInt(weapon.getEnchantLevel() + 3, 1);
					_target.ReceiveManaDamage(_attacker, drainMana);
				} else if (weaponId == 259) {
					if (!Random.chance100(_target.getMr())) {
						_target.ReceiveManaDamage(_attacker, 1);
					}
				}
			}
		}
	}

	// 對目標中毒效果
	public void effectPoision() {
		if (weapon != null) {
			int weaponId = weapon.getItemId();
			if (((weaponId == 13) || (weaponId == 44 // FOD、古代のダークエルフソード
					) || ((weaponId != 0) && _attacker
					.hasSkillEffect(ENCHANT_VENOM))) // エンチャント
														// ベノム中
					&& (Random.chance100(10))) {
				// 通常毒、3秒周期、ダメージHP-5
				L1DamagePoison.doInfection(_attacker, _target, 3000, 5);
			} else {
				// 魔法娃娃效果 - 中毒
				if (L1MagicDoll.getEffectByDoll(_attacker, (byte) 1) == 1) {
					L1DamagePoison.doInfection(_attacker, _target, 3000, 5);
				}
			}
		}
	}

	// ■■■■ 底比斯武器攻撃付加 ■■■■
	public void EffectWeaponMagic() {
		if (weapon != null) {
			int weaponId = weapon.getItemId();
			WeaponEffectTable.getInstance().effect(weaponId,
					weapon.getEnchantLevel(), _attacker, _target);
		}
	}
	
	// ■■■■ 烙印裝備符文系統 ■■■■
	private void effectBrandRuneSet() {
		switch(weapon.getBrandRuneSetId()) {
		case 1:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 45 - (2 * weapon.getEnchantLevel());
				int buffValue = - _attacker.getAc() / 2;
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				_attacker.setSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF, 3000);
				_attacker.addCritical(buffValue);
				_attacker.addAc(buffValue);
				_attacker.setRemindRuneSetId(1);
				_attacker.setRemindBuffValue(buffValue);
				_attacker.sendPackets(new S_OwnCharStatus(_attacker));
				_attacker.sendPackets(new S_SystemMessage("【狂】發動 - 暴擊率提升:" + _attacker.getRemindBuffValue()));
			}
			break;
		case 2:
			;
			break;
		case 3:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 35 - (2 * weapon.getEnchantLevel());
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				_attacker.setSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF, 1000);
				_attacker.setRemindRuneSetId(3);
			}
			if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF)
					&& _attacker.getRemindRuneSetId() == 3) {
				if (!_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_3)) {
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_3, 3000);
				} else {
					_target.killSkillEffectTimer(L1SkillId.BRAND_RUNE_SET_NERF_3);
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_3, 3000);
				}
				if (_target.get_SubRuneSetEffectCount() < 5) {
					_target.set_SubRuneSetEffectCount(_target.get_SubRuneSetEffectCount() + 1);
					int subMr = 20;
					if (_target.getMr() < subMr) {
						subMr = _target.getMr();
					}
					_target.set_SubRuneSetEffect(_target.get_SubRuneSetEffect() + subMr);
					_target.addMr(-subMr);
					_attacker.sendPackets(new S_SystemMessage("【卸】發動 - 扣除魔防:" + subMr));
					if (_target instanceof L1PcInstance) {
						L1PcInstance target = (L1PcInstance) _target;
						target.sendPackets(new S_SPMR(target));
					}
				}
			}
			break;
		case 4:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 35 - (2 * weapon.getEnchantLevel());
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				_attacker.setSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF, 1000);
				_attacker.setRemindRuneSetId(4);
			}
			if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF)
					&& _attacker.getRemindRuneSetId() == 4) {
				if (!_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_4)) {
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_4, 3000);
				} else {
					_target.killSkillEffectTimer(L1SkillId.BRAND_RUNE_SET_NERF_4);
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_4, 3000);
				}
				if (_target.get_SubRuneSetEffectCount() < 5) {
					_target.set_SubRuneSetEffectCount(_target.get_SubRuneSetEffectCount() + 1);
					int subAc = 5;
					if (_target.getAc() + subAc >= 10) {
						subAc = 10 - _target.getAc();
					}
					_target.set_SubRuneSetEffect(_target.get_SubRuneSetEffect() + subAc);
					_target.addAc(subAc);
					_attacker.sendPackets(new S_SystemMessage("【破】發動 - 扣除物防:" + subAc));
					if (_target instanceof L1PcInstance) {
						L1PcInstance target = (L1PcInstance) _target;
						target.sendPackets(new S_OwnCharStatus(target));
					}
				}
			}
			break;
		case 5:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 35 - (2 * weapon.getEnchantLevel());
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				_attacker.setSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF, 1000);
				_attacker.setRemindRuneSetId(5);
			}
			if (_attacker.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF)
					&& _attacker.getRemindRuneSetId() == 5) {
				if (!_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_5)) {
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_5, 3000);
				} else {
					_target.killSkillEffectTimer(L1SkillId.BRAND_RUNE_SET_NERF_5);
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_5, 3000);
				}
				if (_target.get_SubRuneSetEffectCount() < 5) {
					_target.set_SubRuneSetEffectCount(_target.get_SubRuneSetEffectCount() + 1);
					int subDmg = 10;
					_target.set_SubRuneSetEffect(_target.get_SubRuneSetEffect() + subDmg);
					_attacker.sendPackets(new S_SystemMessage("【虛】發動 - 扣除物攻:" + subDmg));
				}
			}
			break;
		case 6:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 35 - (2 * weapon.getEnchantLevel());
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				Thread _timer = new BrandRuneSetEffect_6();
				GeneralThreadPool.getInstance().execute(_timer);
			}
			break;
		case 7:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 45 - (2 * weapon.getEnchantLevel());
				int buffValue = _attacker.getEr() / 2;
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				_attacker.setSkillEffect(L1SkillId.BRAND_RUNE_SET_BUFF, 3000);
				_attacker.setRemindRuneSetId(7);
				_attacker.setRemindBuffValue(buffValue);
				_attacker.sendPackets(new S_SystemMessage("【顛】發動 - 物理傷害提升:" + _attacker.getRemindBuffValue()));
			}
			break;
		case 8:
			if (!_attacker.hasSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER)) {
				int time = 45 - (2 * weapon.getEnchantLevel());
				_attacker.setSkillEffect(L1SkillId.WEAPON_COOLDOWN_TIMER, time * 1000);
				if (!_target.hasSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_8)) {
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_8, 3000);
				} else {
					_target.killSkillEffectTimer(L1SkillId.BRAND_RUNE_SET_NERF_8);
					_target.setSkillEffect(L1SkillId.BRAND_RUNE_SET_NERF_8, 3000);
				}
				_attacker.sendPackets(new S_SystemMessage("【撕】發動 - 撕裂傷口"));
			}
			break;
		case 9:
			;
			break;
		default:
			;
			break;
		}
	}
	
	private class BrandRuneSetEffect_6 extends Thread {
		public void run() {
			int healTimes = 0;
			while(healTimes < 3) {
				try {
					healTimes = healTimes + 1;
					int healValue = _attacker.getMaxHp() / 20;
					int newHp = _attacker.getCurrentHp() + healValue;
					if (newHp > _attacker.getMaxHp()) {
						newHp = _attacker.getMaxHp();
					}
					_attacker.sendPackets(new S_SystemMessage("【癒】發動 - 治癒:" + (newHp - _attacker.getCurrentHp())));
					_attacker.setCurrentHp(newHp);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					break;
				}
			}
		}
	}

}
